package MyGO.dao.impl;

import MyGO.dao.IMedicineDao;
import MyGO.property.Medicine;
import MyGO.utils.FileUtils.FileIO;
import com.fasterxml.jackson.core.type.TypeReference;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static MyGO.utils.PingYinUtils.PinyinUtils.getPinyinAcronym;

public class MedicineImpl implements IMedicineDao {
    private static List<Medicine> MEDICINE_LIST = new ArrayList<>();

    private static HashMap<String,Medicine> MWDICINE_MAP = new HashMap<>();

//    private static HashMap<>

    static {
//        MEDICINE_LIST.add(new Medicine("11145", "青霉素A", "27mg*180粒/盒", "盒", "天津生化医学有限公司", 34.22, getPinyinAcronym("青霉素A"), 100));
//        MEDICINE_LIST.add(new Medicine("11146", "青霉素B", "27mg*180粒/盒", "盒", "天津生化医学有限公司", 44.82, getPinyinAcronym("青霉素B"), 100));
//        MEDICINE_LIST.add(new Medicine("11147", "青霉素C", "27mg*180粒/盒", "盒", "天津生化医学有限公司", 37.27, getPinyinAcronym("青霉素C"), 100));
//        MEDICINE_LIST.add(new Medicine("11233", "复方丹参滴丸", "28mg*160粒/盒", "盒", "沈阳生化医学有限公司", 56.22, getPinyinAcronym("复方丹参滴丸"), 100));
//        MEDICINE_LIST.add(new Medicine("11234", "复方板蓝滴丸", "30mg*120粒/盒", "盒", "沈阳生化医学有限公司", 18.8, getPinyinAcronym("复方板蓝滴丸"), 100));
//        MEDICINE_LIST.add(new Medicine("11235", "复方益气滴丸", "0.04mg*200粒/盒", "盒", "沈阳生化医学有限公司", 38.94, getPinyinAcronym("复方益气滴丸"), 100));
//        MEDICINE_LIST.add(new Medicine("11376", "板蓝根颗粒", "0.5g*15袋/盒", "盒", "提瓦特医药有限公司", 9.8, getPinyinAcronym("板蓝根颗粒"), 100));
//        MEDICINE_LIST.add(new Medicine("11377", "金银花颗粒", "0.5g*15袋/盒", "盒", "提瓦特医药有限公司", 10.8, getPinyinAcronym("金银花颗粒"), 100));
//        MEDICINE_LIST.add(new Medicine("11378", "蒙德颗粒", "0.7g*20袋/盒", "盒", "提瓦特医药有限公司", 29.84, getPinyinAcronym("蒙德颗粒"), 100));
//
//        MWDICINE_MAP.put("11145", MEDICINE_LIST.get(0));
//        MWDICINE_MAP.put("11146", MEDICINE_LIST.get(1));
//        MWDICINE_MAP.put("11147", MEDICINE_LIST.get(2));
//        MWDICINE_MAP.put("11233", MEDICINE_LIST.get(3));
//        MWDICINE_MAP.put("11234", MEDICINE_LIST.get(4));
//        MWDICINE_MAP.put("11235", MEDICINE_LIST.get(5));
//        MWDICINE_MAP.put("11376", MEDICINE_LIST.get(6));
//        MWDICINE_MAP.put("11377", MEDICINE_LIST.get(7));
//        MWDICINE_MAP.put("11378", MEDICINE_LIST.get(8));

        try {
            MEDICINE_LIST = FileIO.readObjectFromJsonFile("Medicine.json", new TypeReference<List<Medicine>>() {});
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            MWDICINE_MAP = FileIO.readObjectFromJsonFile("MedicineMap.json", new TypeReference<HashMap<String,Medicine>>() {});
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Medicine> getMedicineList(){return MEDICINE_LIST;}

    @Override
    public HashMap<String, Medicine> getMedicineMap() {return MWDICINE_MAP; }

    @Override
    public void addMedicine(Medicine medicine) {
        MEDICINE_LIST.add(medicine);
        MWDICINE_MAP.put(medicine.getId(), medicine);
    }
}
